﻿
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports DevComponents.DotNetBar
Imports quanlydulich.Helpers

Partial Public Class KHACHHANG
    Inherits Office2007Form
    Private objDS As DataSet
    Friend WithEvents frmChucVu As CHUCVU
    Friend WithEvents frmLoaiNguoiDung As LOAINGUOIDUNG
    ' Event handler is used to transfer data to receiver
    Public Event Handler(ByVal sender As Object, ByVal e As MyEvent)

    Public Criterial_ID As String
    Public Criterial_Name As String
    Public ModeSearch As Boolean

    Private Sub btnChon_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChon.Click
        Close()
        RaiseEvent Handler(Me, New MyEvent(txtMaKHACHHANG.Text & "#" & txtTenKHACHHANG.Text))
    End Sub

    Private Sub btnNhapMoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapMoi.Click
        EraseTextboxes()
    End Sub

    Public Sub EraseTextboxes()
        txtMaKHACHHANG.Text = ""
        txtTenKHACHHANG.Text = ""
        txtDiaChi.Text = ""
        txtDienThoai.Text = ""
        cbxGioiTinh.SelectedIndex = 0
        txtCMND.Text = ""
        txtQuocTich.Text = ""
        dtgvDataList.ClearSelection()
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim objBUS As KHACHHANGBUS = New KHACHHANGBUS(GlobalVars.g_strConnectionString)
        ' Chi thuc hien xoa, khi user select in grid view
        If txtMaKHACHHANG.Text <> "" Then
            If objBUS.Delete(Integer.Parse(txtMaKHACHHANG.Text)) Then
                LoadData()
                EraseTextboxes()
                MessageBox.Show("A new item has been deleted")
            Else
                MessageBox.Show("Delete failed!")

            End If
        End If
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        ' Neu user chua select in grid view -> thuc hien thao tac insert

        Dim errorText As String
        errorText = ""
        If Not ValidateInput(errorText) Then
            MessageBox.Show(errorText)
            Return
        End If

        If txtMaKHACHHANG.Text = "" Then
            InsertKHACHHANG()
        Else ' Thuc hien thao tac update
            UpdateKHACHHANG()
        End If
    End Sub

    Public Sub InsertKHACHHANG()
        Dim objBUS As KHACHHANGBUS = New KHACHHANGBUS(GlobalVars.g_strConnectionString)
        Dim objDTO As KHACHHANGDTO = New KHACHHANGDTO
        objDTO.TenKH = txtTenKHACHHANG.Text
        objDTO.DiaChi = txtDiaChi.Text
        objDTO.SoDT = txtDienThoai.Text
        objDTO.GioiTinh = IIf(cbxGioiTinh.SelectedIndex = 0, True, False)
        objDTO.CMND = txtCMND.Text
        objDTO.QuocTich = txtQuocTich.Text

        If objBUS.Insert(objDTO) Then
            LoadData()
            EraseTextboxes()
            MessageBox.Show("An item has been Added")
        Else
            MessageBox.Show("Add failed")
        End If
    End Sub
    Public Sub UpdateKHACHHANG()
        Dim objBUS As KHACHHANGBUS = New KHACHHANGBUS(GlobalVars.g_strConnectionString)
        Dim objDto As KHACHHANGDTO = New KHACHHANGDTO
        objDto.ID = Integer.Parse(txtMaKHACHHANG.Text)
        objDto.TenKH = txtTenKHACHHANG.Text
        objDto.DiaChi = txtDiaChi.Text
        objDto.SoDT = txtDienThoai.Text
        objDto.Gioitinh = IIf(cbxGioiTinh.SelectedIndex = 0, True, False)
        objDto.CMND = txtCMND.Text
        objDto.QuocTich = txtQuocTich.Text

        If objBUS.Update(objDto) Then
            LoadData()
            MessageBox.Show("A new item has been updated")
        Else
            MessageBox.Show("Update failed!")
        End If
    End Sub

    Public Function ValidateInput(ByRef errorText As String) As Boolean
        If txtTenKHACHHANG.Text = "" Then
            errorText = "Vui lòng nhập tên khách hàng"
            Return False
        End If
        Return True
    End Function

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Close()
    End Sub

    Private Sub dtgvDataList_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dtgvDataList.CellClick
        Dim selectedRow As Integer = dtgvDataList.CurrentRow.Index
        If selectedRow <> -1 Then
            txtMaKHACHHANG.Text = dtgvDataList.Item(0, selectedRow).Value

            Dim objKHACHHANGBUS As KHACHHANGBUS = New KHACHHANGBUS(GlobalVars.g_strConnectionString)
            Dim lstItem As DataTable = objKHACHHANGBUS.SelectByID(Integer.Parse(txtMaKHACHHANG.Text)).Tables(0)
            If (lstItem.Rows.Count > 0) Then
                txtTenKHACHHANG.Text = lstItem.Rows(0)("TenKH").ToString()
                txtDiaChi.Text = lstItem.Rows(0)("DiaChi").ToString()
                txtDienThoai.Text = lstItem.Rows(0)("SoDT").ToString()
                cbxGioiTinh.SelectedIndex = IIf(Boolean.Parse(lstItem.Rows(0)("GioiTinh").ToString()) = True, 0, 1)
                txtCMND.Text = lstItem.Rows(0)("CMND").ToString()
                txtQuocTich.Text = lstItem.Rows(0)("QuocTich").ToString()
            End If
        End If
    End Sub

    Private Sub KHACHHANG_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()
        CreateGioiTinh()
    End Sub

    ' Load list of KHACHHANG from DB
    Public Sub LoadData()
        Dim objBUS As KHACHHANGBUS = New KHACHHANGBUS(GlobalVars.g_strConnectionString)

        If Not ModeSearch Then
            objDS = objBUS.SelectAll()
        Else
            objDS = objBUS.Search(Criterial_ID, Criterial_Name)
        End If

        ' Neu ton tai du lieu ung voi KHACHHANG table trong DB
        dtgvDataList.AutoGenerateColumns = False
        dtgvDataList.DataSource = objDS.Tables(0)
        dtgvDataList.ClearSelection()
    End Sub

    Public Sub CreateGioiTinh()
        cbxGioiTinh.Items.Clear()

        Dim gioitinh As GioiTinh = New GioiTinh
        gioitinh.Value = 1
        gioitinh.Name = "Nam"
        cbxGioiTinh.Items.Add(gioitinh)

        gioitinh = New GioiTinh
        gioitinh.Value = 0
        gioitinh.Name = "Nữ"
        cbxGioiTinh.Items.Add(gioitinh)
    End Sub
End Class